﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using vngoodlife.Util;
using vngoodlife.Singleton;
using vngoodlife.DTO;
using System.Data.Linq;

namespace vngoodlife.Models
{



    public class MenuList_Validation
    {
        //[System.ComponentModel.DataAnnotations.MetadataType(typeof(vngoodlife.Models.MenuList_Validation))]

        private string menuName;


        [Required(ErrorMessage = "MenuName phải có")]
        public string MenuName
        {
            get { return menuName; }
            set { menuName = value.Trim(); }
        }

        [Required(ErrorMessage = "ControllerName phải có")]
        [RegularExpression(ConstantClass.testNameOfMethod, ErrorMessage = "ControllerName không hợp lệ!")]
        public string ControllerName { get; set; }

        [Required(ErrorMessage = "ActionName phải có")]
        [RegularExpression(ConstantClass.testNameOfMethod, ErrorMessage = "ActionName không hợp lệ!")]
        public string ActionName { get; set; }

        [Required(ErrorMessage = "Location phải có")]
        [RegularExpression(ConstantClass.testPositiveInteger, ErrorMessage = "Location phải là số nguyên dương!")]
        public int Location { get; set; }
    }


    public class MenuListModels
    {

        /// <summary>
        /// Get MenuType
        /// </summary>
        /// <returns></returns>
        internal IQueryable<MenuType> GetMenuType()
        {
            return (DataSingleton.Instance.GetLinQContext().MenuTypes);
        }


        /// <summary>
        /// Get All MenuList filter by Menu Type.
        /// </summary>
        /// <param name="languageID"></param>
        /// <returns></returns>
        public IQueryable<MenuList> GetMenuListView(String menuTypeID)
        {
            if (menuTypeID == "")
            {
                return DataSingleton.Instance.GetLinQContext().MenuLists;
            }
            else
            {
                return DataSingleton.Instance.GetLinQContext().MenuLists.Where(m => m.MenuTypeID == menuTypeID);
            }
        }


        /// <summary>
        /// Get Backend MenuList
        /// </summary>
        /// <returns></returns>
        internal IQueryable<MenuList> GetBackEndMenuList()
        {
            return (DataSingleton.Instance.GetLinQContext().MenuLists.Where(menu => menu.MenuBackEnd == true && menu.MenuVisible == true).OrderBy(menu => menu.Location));
        }


        /// <summary>
        /// Get all menulist filter by language and add option "None"
        /// </summary>
        /// <returns></returns>
        internal List<MenuList> GetMenuListForDDL()
        {
            //Add menu "None" with ID = 0
            MenuList tempMenu = new MenuList();
            tempMenu.MenuListID = 0;
            tempMenu.MenuName = "None";
            List<MenuList> menuList = new List<MenuList>();
            menuList.Add(tempMenu);
            menuList.AddRange(DataSingleton.Instance.GetLinQContext().MenuLists.Where(m => m.MenuTypeID != "UNK" && m.Superior == 0).ToList());

            return (menuList);
        }


        /// <summary>
        /// Insert MenuList in first times. Return 0 when error.
        /// </summary>
        /// <param name="menuListView"></param>
        /// <param name="languageID"></param>
        internal bool MenuListCreate(MenuList menuList, String username, String newMenuImage)
        {
            try
            {

                //menuList.ActionName = menuList.ActionName.Trim();
                menuList.AdminName = username;  
                menuList.MenuListCreateDate = DateTime.Now;
                menuList.MenuListUpdateDate = DateTime.Now;

                if (newMenuImage != null && newMenuImage != "")
                {
                    menuList.Image = newMenuImage;
                }

                DataSingleton.Instance.GetLinQContext().MenuLists.InsertOnSubmit(menuList);
                DataSingleton.Instance.GetLinQContext().MenuLists.Context.SubmitChanges();

                Role_MenuList role_menu = new Role_MenuList();
                role_menu.RoleID = ConstantClass.superadmin;
                role_menu.MenuListID = menuList.MenuListID;
                role_menu.RMUpdateDate = DateTime.Now;
                role_menu.AdminName = username;

                DataSingleton.Instance.GetLinQContext().Role_MenuLists.InsertOnSubmit(role_menu);
                DataSingleton.Instance.GetLinQContext().Role_MenuLists.Context.SubmitChanges();

                //vngoodlife.DTO.General_dto.ClearCacheDataContext(DataSingleton.Instance.GetLinQContext());
            }
            catch (Exception)
            {
                //DataSingleton.Instance.RefreshInstance();
                vngoodlife.DTO.General_dto.ClearCacheDataContext(DataSingleton.Instance.GetLinQContext());
                return false;
            }
            return true;
        }


        /// <summary>
        /// Get a menu based on MenuListID
        /// </summary>
        /// <returns></returns>
        internal MenuList GetMenuByID(int menuListID)
        {
            return (DataSingleton.Instance.GetLinQContext().MenuLists.SingleOrDefault(menu => menu.MenuListID == menuListID));
        }


        /// <summary>
        /// Updatte MenuList
        /// </summary>
        /// <param name="menuListView"></param>
        internal void EditMenuList(MenuList menuList, String newMenuImage)
        {
            try
            {
                MenuList tempMenuList = DataSingleton.Instance.GetLinQContext().MenuLists.SingleOrDefault(m => m.MenuListID == menuList.MenuListID);
                tempMenuList.MenuType = DataSingleton.Instance.GetLinQContext().MenuTypes.Single(u=>u.MenuTypeID== menuList.MenuTypeID);

                tempMenuList.MenuName = menuList.MenuName.Trim();
                tempMenuList.ControllerName = menuList.ControllerName.Trim();
                tempMenuList.ActionName = menuList.ActionName.Trim();
                tempMenuList.Superior = menuList.Superior;
                tempMenuList.MenuBackEnd = menuList.MenuBackEnd;
                tempMenuList.Location = menuList.Location;
                tempMenuList.MenuVisible = menuList.MenuVisible;
                tempMenuList.MenuTitle = menuList.MenuTitle == null? null : menuList.MenuTitle.Trim();
                tempMenuList.MenuDescription = menuList.MenuDescription == null ? null : menuList.MenuDescription.Trim();
                tempMenuList.MenuListCreateDate = DateTime.Now;
                tempMenuList.MenuListUpdateDate = DateTime.Now;

                if (newMenuImage != null && newMenuImage != "")
                {
                    tempMenuList.Image = newMenuImage;
                }

                DataSingleton.Instance.GetLinQContext().MenuLists.Context.SubmitChanges();
                DataSingleton.Instance.GetLinQContext().Refresh(RefreshMode.KeepCurrentValues, menuList);
            }
            catch (Exception)
            {
                //DataSingleton.Instance.RefreshInstance();
                vngoodlife.DTO.General_dto.ClearCacheDataContext(DataSingleton.Instance.GetLinQContext());
            }

        }

        /// <summary>
        /// Delete a menu
        /// </summary>
        /// <param name="menuListID"></param>
        internal void DeleteMenuList(int menuListID, String fullImagePath)
        {

            try
            {
                MenuList menuList = DataSingleton.Instance.GetLinQContext().MenuLists.SingleOrDefault(menu => menu.MenuListID == menuListID);
                
                //Delete physical image of menu
                if (menuList.Image != "")
                {
                    string LocalFile = fullImagePath + menuList.Image;
                    if (System.IO.File.Exists(LocalFile))
                        System.IO.File.Delete(LocalFile);
                }

                DataSingleton.Instance.GetLinQContext().MenuLists.DeleteOnSubmit(menuList);
                DataSingleton.Instance.GetLinQContext().MenuLists.Context.SubmitChanges();
            }
            catch (Exception)
            {
                //DataSingleton.Instance.RefreshInstance();
                vngoodlife.DTO.General_dto.ClearCacheDataContext(DataSingleton.Instance.GetLinQContext());
            }
        }

        /// <summary>
        /// Get Frontend MenuList
        /// </summary>
        /// <returns></returns>
        internal IQueryable<MenuList> GetFrontEndMenuList()
        {
            return (DataSingleton.Instance.GetLinQContext().MenuLists.Where(menu => menu.MenuBackEnd == false && menu.MenuVisible == true 
                    && menu.Superior == 0 && menu.MenuListID != 100 ).OrderBy(menu => menu.Location));
        }


        /// <summary>
        /// Get Frontend Sub MenuList
        /// </summary>
        /// <returns></returns>
        internal IQueryable<MenuList> GetFrontEndSubMenuList(int menuListID)
        {
            return (DataSingleton.Instance.GetLinQContext().MenuLists.Where(menu => menu.MenuBackEnd == false && menu.MenuVisible == true
                    && menu.Superior == menuListID).OrderBy(menu => menu.Location));
        }


        /// <summary>
        /// Get Menu Details
        /// </summary>
        /// <param name="menuListID"></param>
        /// <returns></returns>
        internal MenuList GetMenuSiteMap(int menuListID)
        {
            if (menuListID != 0)
            {
                return DataSingleton.Instance.GetLinQContext().MenuLists.SingleOrDefault(m => m.MenuListID == menuListID && m.MenuVisible == true);
            }
            return null;
        }


        /// <summary>
        /// Get MenuListID by Seo url
        /// </summary>
        /// <param name="menuSeoUrl"></param>
        /// <returns></returns>
        internal int GetArticleTypeIDFromSeoUrl(string menuSeoUrl)
        {
            return DataSingleton.Instance.GetLinQContext().MenuLists.SingleOrDefault(m => m.MenuSeoUrl == menuSeoUrl).MenuListID;
        }


        /// <summary>
        /// Get Seo url of a menu
        /// </summary>
        /// <param name="menuListID"></param>
        /// <returns></returns>
        internal string GetMenuSeoUrl(int menuListID)
        {
            return DataSingleton.Instance.GetLinQContext().MenuLists.SingleOrDefault(m => m.MenuListID == menuListID).MenuSeoUrl;
        }
    }
}